<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'"
             :close-on-click-modal="false"
             :visible.sync="visible"
             @close="close"
             width="50%">
    <div>
      <el-form label-width="120px" :model="dataForm" :rules="dataRule" ref="dataForm"  @keyup.enter.native="dataFormSubmit()">
        <el-form-item label="登录账户名称：" prop="username">
          <el-input v-model="dataForm.username" placeholder="请输入登录账户名称" style="width: 100%"></el-input>
        </el-form-item>
        <el-form-item label="登录手机号码" prop="mobile">
          <el-input v-model="dataForm.mobile" placeholder="请输入登录手机号码" style="width: 100%"></el-input>
        </el-form-item>
        <el-form-item label="登录密码" prop="password">
          <el-input v-model="dataForm.password" @focus="clearPwd" type="password" placeholder="请输入登录密码" style="width: 100%"></el-input>
        </el-form-item>
        <el-form-item class="footer">
          <el-button type="primary" @click.stop="dataFormSubmit" size="small">提交</el-button>
          <el-button type="primary" plain @click.stop="close" size="small">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
  import { isMobile } from '@/utils/validate'
  export default {
    name: 'merchantUser-add-or-update',
    data () {
      var validateMobile = (rule, value, callback) => {
        if (!isMobile(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        dataForm: {
          merchantId: '',
          username: '',
          mobile: '',
          password: ''
        },
        dataRule: {
          username: [
            { required: true, message: '登录账号名称不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '登录密码不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '登录手机号不能为空', trigger: 'blur' },
            { validator: validateMobile, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (merchantId, model) {
        this.dataForm.merchantId = merchantId
        if (model) {
          this.dataForm = JSON.parse(JSON.stringify(model))
        }
        else {
          this.dataForm = {
              merchantId: '',
              username: '',
              mobile: '',
              password: ''
          }
        }
        this.visible = true
      },

      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(this.dataForm.id ? '/v1/admin/merchant/account/update' : '/v1/admin/merchant/add/account'),
              method: 'post',
              data: this.$http.adornData(this.dataForm)
            }).then(({ data }) => {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList', this.page)
                }
              })
            })
          }
        })
      },

      close () {
        this.$refs['dataForm'].resetFields()
        this.visible = false
      },

      clearPwd () {
        this.dataForm.password = ''
      }
    }
  }
</script>

<style lang="scss" scoped>
  .footer{
    display: flex;
    justify-content: flex-end;
    align-items: center;
  }
</style>
